#include <brpc/server.h>
#include <butil/logging.h>
#include "main.pb.h"


// 1.继承EchoService创建一个子类，并实现rpc调用的业务接口
class EchoServiceImpl : public example::EchoService {
public:
    EchoServiceImpl() {}
    ~EchoServiceImpl() {}
    // 实现Echo方法
    void Echo(google::protobuf::RpcController* controller,
        const ::example::EchoRequest* request,
        ::example::EchoResponse* response,
        ::google::protobuf::Closure* done)
    {
        brpc::ClosureGuard rpc_done(done);  // 确保done执行，RAII机制在析构的时候自动调用done->Run()
        std::cout << "收到消息: " << request->message() << std::endl;

        std::string msg = request->message() + "--这是响应!";
        response->set_message(msg);
    }
};

int main(int argc, char* argv[])
{
    // 关闭brpc的日志输出
    logging::LoggingSettings settings;
    settings.logging_dest = logging::LoggingDestination::LOG_TO_NONE;
    logging::InitLogging(settings);
    // 2.构造服务器对象
    brpc::Server server;
    // 3.向服务器对象中，新增EchoService的服务
    EchoServiceImpl echo_service;
    int ret = server.AddService(&echo_service, brpc::ServiceOwnership::SERVER_DOESNT_OWN_SERVICE);    
    if (ret != 0) {
        std::cout << "添加rpc服务失败" << std::endl;
        return -1;
    }

    // 4.启动服务器
    brpc::ServerOptions options;
    options.idle_timeout_sec = -1;  // 连接空闲超时时间-超时后连接会被关闭
    options.num_threads = 1;        // io线程数量
    ret = server.Start(8080, &options);  
    if (ret == -1) {
        std::cout << "启动服务器失败" << std::endl;
        return -1;
    }
    server.RunUntilAskedToQuit();   // 等待服务器退出

    return 0;
}